package httpMessage;

public class HttpRequest {

	// header values

	private String url;
	private String method;
	private String httpVersion;
	private String host;
	private String userAgent;
	private String accept;
	private String acceptLanguage;
	private String encoding;
	private String charset;
	private String connectiontype;
	private StringBuffer dataBuffer;

	// maak HttpRequest van string data
	public HttpRequest(String data) {

		String[] lines = data.split("\r\n");
		System.out.println(lines.length);
		String fline[] = lines[0].split(" ");
		method = fline[0];
		url = fline[1];
		httpVersion = fline[2];

		dataBuffer = new StringBuffer(data.length());
		for (int i = 1; i < lines.length; i++) {
			if (lines[i].startsWith("Host:"))
				host = lines[i].replace("Host: ", "");
			else if (lines[i].startsWith("User-Agent:"))
				userAgent = lines[i].replace("User-Agent: ", "");
			else if (lines[i].startsWith("Accept:"))
				accept = lines[i].replace("Accept: ", "");
			else if (lines[i].startsWith("Accept-Language:"))
				acceptLanguage = lines[i].replace("Accept-Language: ", "");
			else if (lines[i].startsWith("Accept-Charset:"))
				charset = lines[i].replace("Accept-Charset: ", "");
			else if (lines[i].startsWith("Accept-Encoding:"))
				encoding = lines[i].replace("Accept-Encoding: ", "");
			else
				dataBuffer.append(lines[i]);
		}
	}

	public HttpRequest(String url, String method, String host) {
		this.url = url;
		this.method = method;
		this.httpVersion = "HTTP/1.1";
		this.host = host;
		this.accept = "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8";
		//this.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
		this.charset = "UTF-8";
		this.connectiontype = "keep-alive";
		//this.encoding = "gzip, deflate";
	}

	public String getUrl() {
		return url;
	}

	public String getMethod() {
		return method;
	}

	public String getHttpVersion() {
		return httpVersion;
	}

	public String getHost() {
		return host;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public String getAccept() {
		return accept;
	}

	public String getAcceptLanguage() {
		return acceptLanguage;
	}

	public String getEncoding() {
		return encoding;
	}

	public String getCharset() {
		return charset;
	}

	public String getConnectiontype() {
		return connectiontype;
	}

	public StringBuffer getDataBuffer() {
		return dataBuffer;
	}

	@Override
	public String toString() {
		String rString = method + " " + url + " " + httpVersion + "\r\n";
		if (host != null)
			rString += "Host: " + host + "\r\n";
		if (userAgent != null)
			rString += "User-Agent: " + userAgent + "\r\n";
		if (accept != null)
			rString += "Accept: " + accept + "\r\n";
		if (charset != null)
			rString += "Accept-Charset: " + charset + "\r\n";
		if (connectiontype != null)
			rString += "Connection: " + connectiontype + "\r\n";
		if (encoding != null)
			rString += "Accept-Encoding: " + encoding + "\r\n";
		return rString + "\r\n";
	}

}
